<?php

include_once(dirname(__FILE__).'/config.php');

$store = ARC2::getStore($arc_config);

$q1 = <<<EOT
SELECT * WHERE {
	?class a 		<http://desanti.org/schemas/vocabularies#Class> .
	?class ?predicate 	?object .
}
EOT;
$rs1 = $store->query($q1);
if($store->getErrors()) exit($store->getErrors());

$q2 = <<<EOT
SELECT * WHERE {
	?class a 		<http://desanti.org/schemas/vocabularies#Class> .
	?subject ?predicate 	?class .
}
EOT;
$rs2 = $store->query($q2);
if($store->getErrors()) exit($store->getErrors());

$q3 = <<<EOT
SELECT * WHERE {
	?class a 		<http://desanti.org/schemas/vocabularies#Class> .
}
EOT;
$rs3 = $store->query($q3);
if($store->getErrors()) exit($store->getErrors());


$rows = $rs1['result']['rows'];
for($i = 0; $i<sizeof($rows); $i++){
	$r = ereg(".*#(.*)", $rows[$i]['class'], $regs);
	$object = $rows[$i]['object'];
	($rows[$i]['object type'] == 'literal') ? $object = "\"$object\"" : $object = "<$object>";
	$q = <<<EOT
INSERT INTO <http://desanti.org/> {
	<http://desanti.org/classes/{$regs[1]}> <{$rows[$i]['predicate']}> $object .
}
EOT;
	print_r($q); echo "\n";
	//$store->query($q);
	//if($store->getErrors()) exit($store->getErrors());

	$q = <<<EOT
DELETE {
	<{$rows[$i]['class']}> <{$rows[$i]['predicate']}> $object .
}
EOT;
	print_r($q); echo "\n";
	//$store->query($q);
	//if($store->getErrors()) exit($store->getErrors());
}

$rows = $rs2['result']['rows'];
for($i = 0; $i < sizeof($rows); $i++){
	$r = ereg(".*#(.*)", $rows[$i]['class'], $regs);
	$q = <<<EOT
INSERT INTO <http://desanti.org/> {
	<{$rows[$i]['subject']}> <{$rows[$i]['predicate']}> <http://desanti.org/classes/{$regs[1]}> .
}
EOT;
	print_r($q); echo "\n";
	//$store->query($q);
	//if($store->getErrors()) exit($store->getErrors());

	$q = <<<EOT
DELETE {
	<{$rows[$i]['subject']}> <{$rows[$i]['predicate']}> <{$rows[$i]['class']}> .
}
EOT;
	print_r($q); echo "\n";
	//$store->query($q);
	//if($store->getErrors()) exit($store->getErrors());
}

$rows = $rs3['result']['rows'];
for($i = 0; $i < sizeof($rows); $i++){
	$q = <<<EOT
DELETE {
	<{$rows[$i]['class']}> a 		<http://desanti.org/schemas/vocabularies#Class> .
}
EOT;
	print_r($q); echo "\n";
	//$store->query($q);
	//if($store->getErrors()) exit($store->getErrors());
}
?>
